//
//  JMAlternativeView.m
//  OpenSesame
//
//  Created by zhangjiaming on 15/12/30.
//  Copyright © 2015年 OpenSesame. All rights reserved.
//

#import "JMAlternativeView.h"

@implementation JMAlternativeView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:leftBtn];
        self.leftButton = leftBtn;
        [leftBtn setImage:[UIImage imageNamed:@"pinpaizaisheng0"] forState:UIControlStateNormal];
        [leftBtn setImage:[UIImage imageNamed:@"pinpaizaisheng0"] forState:UIControlStateHighlighted];
        [leftBtn setImage:[UIImage imageNamed:@"pinpaizaisheng1"] forState:UIControlStateSelected];
        leftBtn.frame = CGRectMake(0, 0, self.width * 0.5, self.height);
        [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self addSubview:rightBtn];
        self.rightButton = rightBtn;
        [rightBtn setImage:[UIImage imageNamed:@"bianmazaisheng0"] forState:UIControlStateNormal];
        [rightBtn setImage:[UIImage imageNamed:@"bianmazaisheng0"] forState:UIControlStateHighlighted];
        [rightBtn setImage:[UIImage imageNamed:@"bianmazaisheng1"] forState:UIControlStateSelected];
        rightBtn.frame = CGRectMake(self.width * 0.5, 0, self.width * 0.5, self.height);
        [rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void)leftBtnClick:(UIButton *)button {
    self.leftButton.selected = YES;
    self.rightButton.selected = NO;

    //通知代理哪个按钮被点击了
    if ([self.delegate respondsToSelector:@selector(alternativeView:didSelectedButton:)]) {
        [self.delegate alternativeView:self didSelectedButton:button];
    }

}

- (void)rightBtnClick:(UIButton *)button {
    self.leftButton.selected = NO;
    self.rightButton.selected = YES;
    
    //通知代理哪个按钮被点击了
    if ([self.delegate respondsToSelector:@selector(alternativeView:didSelectedButton:)]) {
        [self.delegate alternativeView:self didSelectedButton:button];
    }
}
@end
